package com.example.testcomposeproject.widget

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.BasicText
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.testcomposeproject.PreViewData.PreviewPhoneSize
import com.example.testcomposeproject.R
import com.example.testcomposeproject.ui.theme.AppTheme
import com.example.testcomposeproject.ui.theme.ColorText1E
import com.example.testcomposeproject.ui.theme.cacheAppDimensions


/**
 * @author: hemin
 * @date: 2024/7/12
 * @Desc:
 */

@Preview(showBackground = true)
@Composable
fun PreviewStarTextViewPlus() {
    AppTheme {
        ChooseItem(
            title = "所属路段",
            content = "",
            hintText = "请选择所属路段",
            clickAble = false
        ) {

        }
    }
}

@Composable
fun ChooseItem(
    title: String,
    content: String,
    hintText: String,
    clickAble: Boolean = true,
    mustInput: Boolean = true,
    normalTextColor: Color = ColorText1E,
    modifier: Modifier = Modifier,
    onClickItem: () -> Unit = {}
) {
    if (clickAble) {
        Row(
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically,
            modifier = modifier
                .fillMaxWidth()
                .padding(cacheAppDimensions.mediumPadding)
                .clickable { onClickItem() }

        ) {
            StarTextView(text = title,mustInput=mustInput)

            MiddleTipText(
                text = content,
                hintText = hintText,
                rowScope = this,
                normalTextColor = normalTextColor
                )

            Icon(
                painter = painterResource(id = R.drawable.icon_arrow_right_press),
                contentDescription = null
            )
        }

    } else {
        Row(
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically,
            modifier = modifier
                .fillMaxWidth()
                .padding(cacheAppDimensions.mediumPadding)

        ) {
            StarTextView(text = title,mustInput=mustInput)

            MiddleTipText(
                text = content,
                hintText = hintText,
                rowScope = this,


                )


        }
    }

}


@Preview(showBackground = true)
@Composable
fun StarTextViewPlusPreview() {
    AppTheme {
        StarTextView(text = "所属路段", mustInput = true)
    }
}


@Composable
fun StarTextView(text: String,
                 mustInput:Boolean=true,
                 modifier: Modifier = Modifier,
                 titleWidth: Dp =80.dp) {
    if (mustInput){//带小红星
        val displayText = if (text.endsWith("*")) text else "$text*"
        val annotatedText = buildAnnotatedString {
            append(displayText.dropLast(1))
            withStyle(style = SpanStyle(color = Color.Red)) {
                append("*")
            }
        }

        Box(modifier = modifier) {
            BasicText(
                text = annotatedText,
                modifier = modifier.width(titleWidth),
                style = MaterialTheme.typography.bodyLarge.copy(
                    fontSize = 14.sp,
                    textAlign = TextAlign.Start, color = ColorText1E
                ),
            )
        }
    }else{//不带小红星
        Box(modifier = modifier) {
            BasicText(
                text = text,
                modifier = modifier.width(titleWidth),
                style = MaterialTheme.typography.bodyLarge.copy(
                    fontSize = 14.sp,
                    textAlign = TextAlign.Start, color = ColorText1E
                ),
            )
        }
    }

}




@Composable
fun MiddleTipText(
    text: String,
    hintText: String,
    modifier: Modifier = Modifier,
    rowScope: RowScope,
    normalTextColor: Color = ColorText1E,

    ) {
    with(rowScope) {
        val txtColor = if (text.isEmpty()) Color.Gray else normalTextColor
        val showText = if (text.isEmpty()) hintText else text

        Text(
            text = showText, modifier = modifier
                .weight(1f)
                .padding(start = cacheAppDimensions.mediumPadding),
            color = txtColor, fontSize = 14.sp
        )


    }

}

@Preview(showBackground = true, device = PreviewPhoneSize)
@Composable
fun PreviewMiddleTipText() {
    AppTheme {
        Row {
            MiddleTipText(text = "", hintText = "请选择所属路段", rowScope = this)
        }
    }
}
